package com.qiwx.pigeon.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.data.CageStatusConfig;
import com.qiwx.pigeon.data.params.FertilizeParam;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @description
 * @Author qiwx
 * @Date 2020-05-23 10:25
 * 查崽
 **/
public class CheckBabyOptionView extends LinearLayout {
    @BindView(R.id.rb_chuke3)
    RadioButton rbChuke3;
    @BindView(R.id.rb_wck)
    RadioButton rbWck;
    @BindView(R.id.rb_no_egg_baby)
    RadioButton rbNoEggBaby;
    @BindView(R.id.rb_cz_bfd)
    RadioButton rbCzBfd;
    @BindView(R.id.rb_cz_cpd)
    RadioButton rbCzCpd;
    @BindView(R.id.group_check_baby)
    GridRadioGroup groupCheckBaby;
    @BindView(R.id.rb_baby_cz)
    RadioButton rbBabyCz;
    @BindView(R.id.rb_baby_empty)
    RadioButton rbBabyEmpty;
    @BindView(R.id.group_cage_status)
    RadioGroup groupCageStatus;
    @BindView(R.id.sw_remind)
    SwitchCompat swRemind;
    @BindView(R.id.lin_remind)
    LinearLayout linRemind;
    @BindView(R.id.lin_group_options_checkbaby)
    LinearLayout linGroupOptionsCheckbaby;

    public CheckBabyOptionView(Context context) {
        this(context, null);
    }

    public CheckBabyOptionView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CheckBabyOptionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initEvent();
    }

    private void initView(Context context) {
        LayoutInflater mInflater = LayoutInflater.from(context);
        View view = mInflater.inflate(R.layout.group_check_baby_layout, null);
        ButterKnife.bind(this, view);
        addView(view);
    }

    private void initEvent() {
        swRemind.setOnCheckedChangeListener((btn, ischeck) -> {
            if (ischeck) {
                btn.setText("关闭提醒");
            } else {
                btn.setText("打开提醒");

            }
        });
        rbChuke3.setOnCheckedChangeListener((btn, ischeck) -> {
            if (ischeck) {
                rbBabyEmpty.setChecked(true);
                rbBabyCz.setVisibility(VISIBLE);
            }

        });
        rbWck.setOnCheckedChangeListener((btn, ischeck) -> {

            if (ischeck) {
                rbBabyEmpty.setChecked(true);
                rbBabyCz.setVisibility(VISIBLE);
            }

        });
        rbNoEggBaby.setOnCheckedChangeListener((btn, ischeck) -> {
            if (ischeck) {
                rbBabyEmpty.setChecked(true);
                rbBabyCz.setVisibility(GONE);
            }
        });
        rbCzBfd.setOnCheckedChangeListener((btn, isCheck) -> {
            if (isCheck) {
                rbBabyEmpty.setChecked(true);
                rbBabyCz.setVisibility(GONE);
            }

        });
        rbCzCpd.setOnCheckedChangeListener((btn, isCheck) -> {
            if (isCheck) {
                rbBabyEmpty.setChecked(true);
                rbBabyCz.setVisibility(GONE);
            }
        });
    }

    public void resetOptions() {
        rbChuke3.setChecked(true);
        rbBabyEmpty.setChecked(true);
    }

    public FertilizeParam getParam(int amount) {
        FertilizeParam param = new FertilizeParam();
        int ckAmount;
        int babyStatus = 0;
        if (rbChuke3.isChecked()) {
            ckAmount = 3;
            babyStatus = CageStatusConfig.CHECK_BABY_CK3;
        } else if (rbWck.isChecked()) {
            ckAmount = 0;
            babyStatus = CageStatusConfig.CHECK_BABY_WCK;
        } else if (rbNoEggBaby.isChecked()) {
            ckAmount = 0;
            babyStatus = CageStatusConfig.CHECK_BABY_NO_EGG_BABY;
            param.setWudan(amount);
        } else {
            ckAmount = 0;
            babyStatus = CageStatusConfig.CHECK_BABY_CK1;
        }
        param.setIs_remind(swRemind.isChecked());
        param.setEmptyCage(rbBabyEmpty.isChecked());
        param.setCk_amount(ckAmount);
        param.setEgg_status(babyStatus);
        param.setBfd(rbCzBfd.isChecked() ? amount : 0);
        param.setBreaken(rbCzCpd.isChecked() ? amount : 0);
        param.setWudan(rbNoEggBaby.isChecked() ? amount : 0);
        return param;

    }
}
